/*******************************************************************************
 * 名称： 3. 无重复字符的最长子串
 * 地址： https://leetcode.cn/problems/longest-substring-without-repeating-characters/description/
 ******************************************************************************/

/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function (s) {
  const strLen = s.length;
  if (!strLen) return 0;

  let maxLen = 0;
  const objSet = new Set();
  let rPoint = -1;

  for (i = 0; i < strLen; i++) {
    const targetStr = s.charAt(i - 1);

    if (i != 0) {
      objSet.delete(targetStr);
    }

    while (rPoint + 1 < strLen && !objSet.has(s.charAt(rPoint + 1))) {
      objSet.add(s.charAt(rPoint + 1));
      ++rPoint;
    }

    maxLen = Math.max(maxLen, objSet.size);
  }

  return maxLen;
};

module.exports = {
  name: '3. 无重复字符的最长子串',
  testFc: lengthOfLongestSubstring,
};
